package com.shop.dao.impl;

import com.shop.dao.OrderDao;
import com.shop.entity.Order;
import com.shop.entity.ShowPageT;
import com.shop.tools.JdbcTool;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * com.shop.dao.impl文档说明
 *
 * @author Angluzha
 * @version 2022/9/17
 */
public class OrderDaoImpl implements OrderDao {
    @Override
    public Order selectOne(Order order) throws SQLException {
        String sql = "select * from s_order where id =?";
        return JdbcTool.selectOne(Order.class,sql,order.getId());
    }

    @Override
    public boolean update(Order o) {
        String sql = "update s_order set user=?,goods=?,quantity=?,spend=?,statu=?,address=?,name=?,phone=?,pay=?,transport=?";
        return JdbcTool.write(sql,o.getUser(),o.getGoods(),o.getQuantity(),o.getSpend(),o.getStatu(),o.getAddress(),o.getName(),o.getPhone(),o.getPay(),o.getTransport())>0;
    }

    @Override
    public boolean insert(Order o) {
        String sql = "insert into s_order(user,goods,quantity,spend,address,name,phone,pay,transport) values(?,?,?,?,?,?,?,?,?)";
        return JdbcTool.write(sql,o.getUser(),o.getGoods(),o.getQuantity(),o.getSpend(),o.getAddress(),o.getName(),o.getPhone(),o.getPay(),o.getTransport())>0;
    }

    @Override
    public boolean deleteById(Integer id) {
        String sql = "delete from s_order where id= ?";
        return JdbcTool.write(sql,id)>0;
    }

    @Override
    public List<Order> selectAll() throws SQLException {
        String sql = "select * from s_order";
        return JdbcTool.selectList(Order.class,sql);
    }

    @Override
    public ShowPageT<Order> selectPage(ShowPageT<Order> page) throws SQLException {
        String sql = "SELECT goods.pnguri,goods.gname,goods.des,s_user.username,s_order.* \n" +
                "FROM s_order LEFT JOIN goods ON s_order.goods = goods.id \n" +
                "LEFT JOIN s_user ON s_order.`user` = s_user.id LIMIT ?,?";
        String sql2 = "select count(1) from s_order";
        page.setCount(JdbcTool.selectResult(Integer.class,sql2));
        page.setData(JdbcTool.selectList(Order.class,sql,page.getSize()*(page.getPage()-1),page.getSize()));
        return page;
    }

    @Override
    public int delCheck(List<Integer> list) {
        StringBuffer sql = new StringBuffer("delete from s_order where id in(");
        list.forEach(e->{
            sql.append("?,");
        });
        sql.deleteCharAt(sql.length()-1).append(")");
        return JdbcTool.write(String.valueOf(sql),list.toArray());
    }
    public Integer insertGetKey(Order o) throws SQLException {
        String sql = "insert into s_order(user,quantity,spend,address,name,phone,pay,transport) values(?,?,?,?,?,?,?,?)";
        return JdbcTool.insertGet(sql, o.getUser(), o.getQuantity(), o.getSpend(), o.getAddress(), o.getName(), o.getPhone(), o.getPay(), o.getTransport());
    }
}
